package sip

import (
	"bytes"
	"encoding/xml"
	"gitee.com/slh1992/plugin-sip/utils"
	"golang.org/x/net/html/charset"
)

type LiveType int
type ExecType int

const (
	LIVE_STREAM_PUSH LiveType = iota
	LIVE_CDN_PUSH
	ExecStop ExecType = iota
	ExecPlay
)

func (l LiveType) String() string {
	switch l {
	case LIVE_STREAM_PUSH:
		return "LIVE_STREAM_PUSH"
	case LIVE_CDN_PUSH:
		return "LIVE_CDN_PUSH"
	}
	return "LIVE_UNKNOWN"
}
func (e ExecType) String() string {
	switch e {
	case ExecStop:
		return "ExecStop"
	case ExecPlay:
		return "ExecPlay"
	}
	return "ExecUnknown"
}

type CmdCdnLiver struct {
	XMLName  xml.Name `xml:"Control"`
	CmdType  string
	DeviceID string
	SN       string
	LiveType LiveType //直播类型： 0：流媒体推流 1：CDN推流
	ExecCmd  ExecType // 执行类型： 0:stop 1:start
	Endtimes uint64   //结束时间
	StreamId string   //流ID
}

type CmdCdnLiveResult struct {
	XMLName  xml.Name `xml:"Reponse"`
	CmdType  string
	DeviceID string
	SN       string
	Result   string
	Extends  string //扩展数据
}

func (c *CmdCdnLiver) String() string {
	xmlStr, err := XmlMashal(c)
	if err != nil {
		return ""
	}
	return xmlStr
}

func (c *CmdCdnLiver) Decode(data string) error {
	decoder := xml.NewDecoder(bytes.NewReader([]byte(data)))
	decoder.CharsetReader = charset.NewReaderLabel
	err := decoder.Decode(c)
	if err != nil {
		err = utils.DecodeGbk(c, []byte(data))
		if err != nil {
			return err
		}
	}
	return nil
}

func (r *CmdCdnLiveResult) String() string {
	xmlStr, err := XmlMashal(r)
	if err != nil {
		return ""
	}
	return xmlStr
}

func (r *CmdCdnLiveResult) Decode(data string) error {
	decoder := xml.NewDecoder(bytes.NewReader([]byte(data)))
	decoder.CharsetReader = charset.NewReaderLabel
	err := decoder.Decode(r)
	if err != nil {
		err = utils.DecodeGbk(r, []byte(data))
		if err != nil {
			return err
		}
	}
	return nil
}
